package com.htj.filter;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//定义登录拦截器

/**
 * 拦截器,在dispatch.xml里面定义了(拦截全部请求,放行登录请求)
 * 拦截其他请求后,判断是否登录,如果没有登录那么重定向到登录页
 */
public class LoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //System.out.println("request.getServletPath():"+request.getServletPath());
        //如果没有登录,那么重定向到登录页,因为转发不会改变url地址
        if(request.getSession().getAttribute("user") == null){
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            return false;
        }
        return true;
    }
}
